
<template>
  <div class="my-slider"
       ref="slider">
    <div class="slider-group"
         ref="sliderGroup">
      <slot></slot>
    </div>
    <!-- 轮播点 -->
    <div class="dots">
      <span v-for="(dot, index) in dots"
            :key="index"
            :class="{ active: currentDotsIndex === index }"
            class="dot"></span>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { myDOM } from "common/js/dom.js";
import BScroll from "better-scroll";
export default {
  data () {
    return {
      dots: [],
      currentDotsIndex: 0
    }
  },
  props: {
    loop: {
      type: Boolean,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    interval: {
      type: Number,
      default: 4000
    },
    showDot: {
      type: Boolean,
      default: true
    },
    click: {
      type: Boolean,
      default: true
    },
    threshold: {
      type: Number,
      default: 0.3
    },
    speed: {
      type: Number,
      default: 400
    }
  },
  mounted () {
    setTimeout(() => {
      this._setSliderWidth()
      this._initDots()
      this._initSlider()

      if (this.autoPlay) {
        this._initPlay()
      }

      // 当窗口尺寸改变时，重新计算轮播宽度
      window.addEventListener('resize', () => {
        if (!this.slider) {
          return
        }
        this._setSliderWidth(true)
        this.slider.refresh()
      })
    }, 20)
  },
  methods: {
    _setSliderWidth () {
      this.children = this.$refs.sliderGroup.children;
      let width = 0;
      let sliderWidth = this.$refs.slider.clientWidth;
      for (let i = 0, len = this.children.length; i < len; i++) {
        let child = this.children[i];
        myDOM.addClass(child, "slider-item");
        child.style.width = sliderWidth + "px";
        width += sliderWidth;
      }
      if (this.loop) {
        width += 2 * sliderWidth
      }
      this.$refs.sliderGroup.style.width = width + "px"
    },
    _initDots () {
      this.dots = new Array(this.children.length)
    },
    _initSlider () {
      console.log(this.loop);

      this.slider = new BScroll(this.$refs.slider, {
        scrollX: true,
        scrollY: false,
        momentum: false,
        snap: {
          loop: this.loop,
          threshold: this.threshold,
          speed: this.speed
        },
        bounce: false,
        stopPropagation: true,
        click: this.click
      })
      this.slider.on('scrollEnd', () => {

     
        
        let nowIndex = this.slider.getCurrentPage().pageX
        this.currentDotsIndex = nowIndex;
        if (this.autoPlay) {

          this._initPlay()
        }
      })
    },
    // 自动播放
    _initPlay () {
      clearTimeout(this.timer);
      let nextIndex = this.currentDotsIndex + 1;
      if (this.loop) {
        nextIndex += 1
      }
      this.timer = setTimeout(() => {
        this.slider.next()
      }, this.interval)
    },

  },
  destroyed () {
    console.log(1111);
    
    clearTimeout(this.timer);
  }

}
</script>
<style lang="scss" scoped>
@import '~common/scss/mymixin.scss';

.my-slider {
  min-height: 2px;
  .slider-group {
    position: relative;
    overflow: hidden;
    white-space: nowrap;
    .slider-item {
      float: left;
      box-sizing: border-box;
      overflow: hidden;
      text-align: center;
      a {
        display: block;
        width: 100%;
        overflow: hidden;
        text-decoration: none;
      }
      img {
        display: block;
        width: 100%;
      }
    }
  }
  .dots {
    position: absolute;
    right: 0;
    left: 0;
    bottom: 24px;
    text-align: center;
    font-size: 0;
    .dot {
      display: inline-block;
      margin: 0 8px;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      background: $color-text-l;
      &.active {
        width: 40px;
        border-radius: 10px;
        background: $color-text-ll;
      }
    }
  }
}
</style>
